<?php
	include('config.php');
	
	$thread = 1;
	if(isset($_GET['thread'])){
		$thread = addslashes($_GET['thread']);
	}
	
	$query = 'SELECT * FROM THREADS LIMIT ' . (($thread - 1) * 30) . ', ' . ($thread * 30);	
	$results = mysql_query($query);
	
	echo '<div style="float: left; padding-top: 20px;">';
	echo '<h2>Android Dev Threads</h2>';
	echo '<table border="4" id="generic">';
	echo '<tr><td><b>Topic</b></td> <td><b>Original Poster</b></td> <td><b>Last Post</b></td> <td><b>Post Count</b></td></tr>';
	
	while($row = mysql_fetch_array($results)) {
		echo '<tr><td><a href="index.php?page=threads&id=' . $row['thread_id'] . '">' . $row['topic'] . '</a></td><td>' . 
		$row['username'] . '</td><td>' . 
		$row['last_post'] . '</td><td>' . 
		$row['post_count'] . 	'</td></tr>';
	}	
	
	echo '</table>';
	echo '</div>';
	echo '<br /><br />';
	
	$query = 'SELECT COUNT(*) "count" FROM THREADS';
	$results = mysql_query($query);
	$row = mysql_fetch_array($results);
	
	$pages = ceil($row['count']/30);
	
	echo '<div style="float: left; padding-top: 20px;">';
	if($thread > 1)
		echo '<a href=index.php?page=viewthreads&thread=' . ($thread - 1) . '><<</a> ';
	
	for($i = 0; $i < $pages; $i ++)
		echo ' <a href=index.php?page=viewthreads&thread=' . ($i + 1) . '>' . ($i + 1) . '</a> ';
		
	if($thread < $pages)
		echo ' <a href=index.php?page=viewthreads&thread=' . ($thread + 1) . '>>></a>';
		
	if(isset($_COOKIE['session_id']) && isset($_COOKIE['user_id'])){
		echo '<br /><br /><br /><br />
				<h2>Start a New Thread</h2>';
		echo '<form name="new_thread" action="javascript: void(0);" method="POST">
			<b>Topic</b><br /><input name="topic" type="text" cols=84 /><br /><br />
			<b>Initial Comment</b><br /><textarea name="comment" cols=84 rows=6></textarea><br />' .
			'<input type="submit" value="Create Thread" onclick="validateThread()"></form>';
	}
	
	echo '</div>';
	
	mysql_close($mysql_handle);
?>